package com.region.integral.common_user.obtain.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.region.integral.common_user.obtain.dao.IIntegralGroupPeopleRise;
import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.common_user.pojo.CUIEntity;
import com.region.integral.common_user.xml.Regulation;
import com.region.integral.common_user.xml.dto.CommentWeiBo;
import com.region.integral.common_user.xml.dto.GroupPeopleRise;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.XMLRegulationUtil;
@Component("iPeopleRise")
public class IntegralGroupPeopleRiseImpl extends BaseHibernate implements
		IIntegralGroupPeopleRise {

	@Override
	public void groupPeopleRise(int userid, String obtainWay,IntegralData integralData) throws IntegralException {
		CUIEntity cuiEntity = getCUIEntity(userid);
		if (cuiEntity != null) {
			int groupPeople = cuiEntity.getGroupPeopleNum();
			
			// 获取积分规则
			Regulation regulation = XMLRegulationUtil.unmarshaller();
			List<GroupPeopleRise> groupPeopleRises = regulation.getGrouppeoplerise();
			for (GroupPeopleRise groupPeopleRise : groupPeopleRises) {
				if (groupPeople < groupPeopleRise.getBelow()) {
					int currIntegral = cuiEntity.getCurrIntegral();
					int maxIntegral = cuiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ groupPeopleRise.getIntegral();
					cuiEntity.setCurrIntegral(totalIntegral);
					if (maxIntegral < totalIntegral) {
						cuiEntity.setMaxIntegral(totalIntegral);
					}
					integralData.setSuccee(true);
					integralData.setObtainIntegral(groupPeopleRise.getIntegral());
					hibernateTemplate.update(cuiEntity);
				}
				if(groupPeopleRise.getOver()!=0 && groupPeople >= groupPeopleRise.getOver()&& groupPeople % groupPeopleRise.getRate() == 0){
					int currIntegral = cuiEntity.getCurrIntegral();
					int maxIntegral = cuiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ groupPeopleRise.getIntegral();
					cuiEntity.setCurrIntegral(totalIntegral);
					if (maxIntegral < totalIntegral) {
						cuiEntity.setMaxIntegral(totalIntegral);
					}
					integralData.setSuccee(true);
					integralData.setObtainIntegral(groupPeopleRise.getIntegral());
					hibernateTemplate.update(cuiEntity);
				}
				if(groupPeople !=0 && groupPeople == groupPeopleRise.getHundred()){
					int currIntegral = cuiEntity.getCurrIntegral();
					int maxIntegral = cuiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ groupPeopleRise.getIntegral();
					if (maxIntegral < totalIntegral) {
						cuiEntity.setMaxIntegral(totalIntegral);
					}
					cuiEntity.setCurrIntegral(totalIntegral);
					hibernateTemplate.update(cuiEntity);
				}
				if(groupPeopleRise.getGetModerator()!=null && !"".equals(groupPeopleRise.getGetModerator())&&groupPeopleRise.getGetModerator().equals(obtainWay)){
					int currIntegral = cuiEntity.getCurrIntegral();
					int maxIntegral = cuiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ groupPeopleRise.getIntegral();
					if (maxIntegral < totalIntegral) {
						cuiEntity.setMaxIntegral(totalIntegral);
					}
					cuiEntity.setCurrIntegral(totalIntegral);
					hibernateTemplate.update(cuiEntity);
				}
			}
			cuiEntity.setGroupPeopleNum(groupPeople + 1);
			hibernateTemplate.update(cuiEntity);
		}
	}
}
